import {
    nextTick,
    msg,
    open,
    setPageNumber,
    confirm
} from '../../common/utils.js';
import Vue from 'vue';
import {
    serverPath,
    json,
    currentPath
} from '../../common/config.js';
import page from '../../../components/page.vue';
import ClassService from '../services/classService.js';
import SchoolService from '../services/schoolService.js';

window.Module= function(){
    let classService = new ClassService();
    let schoolService = new SchoolService();
    return new Vue({
        el:'#indexController',
        data:{
            pager: {
                pageIndex: 1,
                pageSize: 10,
                pageCount: 0,
                totalCount: 0,
                pageNumber: []
            },
            list: [],
            searchCondition: {
                name: '',
                schoolsId:''
            },
            schools:[]
        },
        methods:{
            init: async function () {
                await this.loadData();
                await this.loadSchools();
            },
            showAddPage: async function () {
                let options = {
                    title: "新增",
                    content: `${currentPath}dialog.html#/ClassPage/AddPage`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
                await this.loadData();
            },
            pageChange: async function (pageIndex, e) {
                this.pager.pageIndex = pageIndex;
                await this.loadData();
            },
            loadData: async function () {
                let res = await classService.getClasses(this.pager.pageIndex, this.pager.pageSize,this.searchCondition);
                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.list = response.data.data;
                        for (let item in response.data) {
                            if ('data' !== item) {
                                this.pager[item] = res.body.data[item];
                            }
                        }
                        setPageNumber(this.pager);
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            loadSchools: async function () {
                let res = await schoolService.getSchools(1,100);
                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.schools = response.data.data;
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            setValid: async function (item) {
                if (!item)
                    return;
                await confirm(`确定要修改有效值(班级名称:${item.name})？`);
                let res = await classService.updateClassesValid(item.id);
                if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        await this.loadData();
                    }
                    else {
                        await msg(response.message);
                    }
                }

            },
            deleteClass: async function (item) {
                if (!item)
                    return;
                await confirm(`确定要删除班级(班级名称:${item.name})？`);
                let res = await classService.deleteClasses(item.id);
                if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        await this.loadData();
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            detailClass: async function (item) {
                let options = {
                    title: "学校详情",
                    content: `${currentPath}dialog.html#/ClassPage/DetailPage?id=${item.id}`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
            },
            editClass: async function (item) {
                let options = {
                    title: "编辑学校",
                    content: `${currentPath}dialog.html#/ClassPage/EditPage?id=${item.id}`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
                await this.loadData();
            }

        },
        components: {
            "page": page
        }
    });
};